import {userInfo} from '@/api/UserInformation/userInfo'
import { onMounted,ref } from 'vue'
import axios from 'axios'
import { useRouter } from 'vue-router'
import router from '@/router'
export let list = ref([] as any[])
let selectedKeys = ref<string[]>([])
function jkff(){
    userInfo().then((res:any)=>{
         console.log(res)
         list.value=res.list
         let children=res.list[0].children
         let menu_id=children.length >0 ? children[0].menu_id:res.list[0].menu_id
         selectedKeys.value=[menu_id]
         DynamicRouter(res.list)
    }).catch((err)=>{
        console.log(err)
    }) 
    // axios.get('https://d33a5037-6c63-4e92-8bb1-30018ab701ea.bspapp.com/http/admin/system/menu/list',{headers:{"authorization":"Bearer "+localStorage.token}})
    // .then((res)=>{
    //     console.log(res)
    //     list.value=res.data.list
    // })
}
const pages = import.meta.glob('../pages/**/**.vue')
export function DynamicRouter(list:any){
 list.forEach((z:any) => {
     router.addRoute('index',{
        path:z.url,
        name:z.url,
        component:pages['..'+z.url+'.vue'],
       
     })
     if(z.children){
    DynamicRouter(z.children)
    
     }
 });
}
console.log(pages)
export function fzgz(){
    let router=useRouter()
    function djtz(e:any){
        console.log(e)                   //index.vue 里面的  url  是自定义的 :url="sjj.url"
        router.push(e.item.url)
       
        console.log(e.item.url)
       }
onMounted(()=>{
    jkff()
})
return {
    list,
    selectedKeys,
    djtz
}
}